package com.lxy.StretchingBack;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;

public class MainActivity extends Activity {
	
	LinearLayout l_up;
	LinearLayout l_down;
	LinearLayout l_centre; 
	LinearLayout l_left; 
	LinearLayout l_right; 
	
	int y;
	int x;
	int state;//1down   2up   3right   4left
	//定时器
	ScheduledExecutorService service;
	Runnable runnable;
	 
	//回缩动画
	Handler handler=new Handler(){
		@Override
		public void handleMessage(Message msg) {
			if(msg.what==0){
				service.shutdown();
			}else if(msg.what==1){
				LayoutParams params=l_up.getLayoutParams();
				
				params.height-=10;
				if(params.height<=0){
					params.height=0;
					service.shutdown();
					state=0;
				}
				l_up.setLayoutParams(params);
			}else if(msg.what==2){
				LayoutParams params=l_down.getLayoutParams();
				
				params.height-=10;
				if(params.height<=0){
					params.height=0;
					service.shutdown();
					state=0;
				}
				l_down.setLayoutParams(params);
			}else if(msg.what==3){
				LayoutParams params=l_right.getLayoutParams();
				
				params.width-=10;
				if(params.width<=0){
					params.width=0;
					service.shutdown();
					state=0;
				}
				l_right.setLayoutParams(params);
			}else if(msg.what==4){
				LayoutParams params=l_left.getLayoutParams();
				
				params.width-=10;
				if(params.width<=0){
					params.width=0;
					service.shutdown();
					state=0;
				}
				l_left.setLayoutParams(params);
			}
			
		}
	};
  
	
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        l_up=(LinearLayout) findViewById(R.id.l_up);
        l_down=(LinearLayout) findViewById(R.id.l_down);
        l_centre=(LinearLayout) findViewById(R.id.l_centre);
        l_left=(LinearLayout) findViewById(R.id.l_left);
        l_right=(LinearLayout) findViewById(R.id.l_right);
        
        //手势监听
        l_centre.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				//回缩动画执行中
				if(service!=null&&!service.isShutdown()){
					return true;
				}
				
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					y=(int) event.getY();
					x=(int) event.getX();
					
					break;
				case MotionEvent.ACTION_MOVE:{
					int yy=(int) (event.getY()-y);
					int xx=(int) (event.getX()-x);
				if(state==0){//第一次判断拉动方向
					if(Math.abs(yy)>Math.abs(xx)){
						//up down
						if(yy>0){
							//down
							state=1;
							down(yy);
						}else{
							//up
							state=2;
							up(yy);
						}
					}else{
						//left right
						if(xx>0){
							//left
							state=4;
							left(xx);
						}else{
							//right
							state=3;
							right(xx);
						}
					}
				}else{//按既定方向拉动
					switch(state){
					case 1:
						down(yy);
						break;
					case 2:
						up(yy);
						break;
					case 3:
						right(xx);
						break;
					case 4:
						left(xx);
						break;
					}
				}
				y=(int) event.getY();
				x=(int) event.getX();
				}break;
				case MotionEvent.ACTION_UP:
					runnable=new Runnable() {
						@Override
						public void run() {
							//回缩动画
							handler.sendEmptyMessage(state);
						}
					};
					service = Executors .newSingleThreadScheduledExecutor();
					service.scheduleAtFixedRate(runnable, 0, 10, TimeUnit.MILLISECONDS);
					break;
				}
				return true;
			}
		});
    }
    //上拉
	private void up(int h){
		h=-h;
		LayoutParams params = l_down.getLayoutParams();
		h*=0.8;
		params.height+=h;
		l_down.setLayoutParams(params);
	}
	//下拉
	private void down(int h){
		LayoutParams params = l_up.getLayoutParams();
		h*=0.8;
		params.height+=h;
		l_up.setLayoutParams(params);
	}
	//左拉
	private void left(int h){
		LayoutParams params = l_left.getLayoutParams();
		h*=0.8;
		params.width+=h;
		l_left.setLayoutParams(params);
	}
	//右拉
	private void right(int h){
		h=-h;
		LayoutParams params = l_right.getLayoutParams();
		h*=0.8;
		params.width+=h;
		l_right.setLayoutParams(params);
	}
}
